<script setup lang="ts">
// 一个页面两用
// 1. 正常增删改查效果
// 2. 正常增删改查效果+多一个选中效果
// 进入页面时传参
import { getPatientList } from '@/services/user';
import { useConsultStore } from '@/stores/modules/consult';
import type { PatientList } from '@/types/user';
import { showNotify } from 'vant';
import { computed, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
// import { onMounted } from 'vue';


// 定义数据
const patientList = ref<PatientList>([])
// 请求服务端获取患者列表（查询  增加删除修改）
async function getList () {
  const res = await getPatientList()
  // console.log(res.data)
  patientList.value = res.data
}

// onMounted(() => {
//   getList()
// })
getList() // setup钩子更快


// this.$route.query.isChange
const route = useRoute()
// console.log(route.query.isChange==='1')
const isChange = computed(() => route.query.isChange==='1')


// 选中患者的id
const patientId = ref<string>('')

const select = (id: string) => {
  // isChange&&(patientId=item.id)
  patientId.value = isChange.value ? id : ''
}

const consultStore = useConsultStore()
const router = useRouter()
const next = () => {
  if(!patientId.value) {
    showNotify('请选择患者')
    return
  }

  // 记录患者id
  consultStore.setPatientId(patientId.value)
  // 跳转
  router.push('/consult/pay')
}
</script>

<template>
  <div class="patient-page">
    <!-- <cp-nav-bar title="家庭档案"></cp-nav-bar> -->
    <van-nav-bar :title="isChange?'选择患者':'家庭档案'" left-arrow />

    <div class="patient-change" v-if="isChange">
      <h3>请选择患者</h3>
      <p>以便医生给出正确的治疗</p>
    </div>


    <div class="patient-list">
      <div @click="select(item.id)" :class="{ selected: patientId===item.id }" v-for="item in patientList" :key="item.id" class="patient-item">
        <div class="info">
          <span class="name">{{ item.name }}</span>
          <!-- 身份证号需要脱敏-去除敏感信息 -->
          <!-- item.idCard利用正则表达式保留前六位 后四位 其余用*代替 -->
          <span class="id">{{ item.idCard.replace(/^(\d{6})\d+(\d{4})$/, '$1******$2') }}</span>
          <!-- <span class="id">{{ item.idCard }}</span> -->
          <span>{{ item.genderValue }}</span>
          <span>{{item.age}}岁</span>
        </div>
        <div class="icon"><cp-icon name="user-edit" /></div>
        <div class="tag" v-if="item.defaultFlag===1">默认</div>
      </div>

      <div class="patient-add">
        <cp-icon name="user-add" />
        <p>添加患者</p>
      </div>
      <div class="patient-tip">最多可添加 6 人</div>
    </div>

    <div style="padding: 0 15px">
      <van-button @click="next" type="primary" block round>下一步</van-button>

    </div>
  </div>
</template>

<style lang="scss" scoped>
.patient-page {
  padding: 46px 0 80px;
}
.patient-list {
  padding: 15px;
}
.patient-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: var(--cp-bg);
  border-radius: 8px;
  margin-bottom: 15px;
  position: relative;
  border: 1px solid var(--cp-bg);
  transition: all 0.3s;
  overflow: hidden;
  .info {
    display: flex;
    flex-wrap: wrap;
    flex: 1;
    span {
      color: var(--cp-tip);
      margin-right: 20px;
      line-height: 30px;
      &.name {
        font-size: 16px;
        color: var(--cp-text1);
        width: 80px;
        margin-right: 0;
      }
      &.id {
        color: var(--cp-text2);
        width: 180px;
      }
    }
  }
  .icon {
    color: var(--cp-tag);
    width: 20px;
    text-align: center;
  }
  .tag {
    position: absolute;
    right: 60px;
    top: 21px;
    width: 30px;
    height: 16px;
    font-size: 10px;
    color: #fff;
    background-color: var(--cp-primary);
    border-radius: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  &.selected {
    border-color: var(--cp-primary);
    background-color: var(--cp-plain);
    .icon {
      color: var(--cp-primary);
    }
  }
}
.patient-add {
  background-color: var(--cp-bg);
  color: var(--cp-primary);
  text-align: center;
  padding: 15px 0;
  border-radius: 8px;
  .cp-icon {
    font-size: 24px;
  }
}
.patient-tip {
  color: var(--cp-tag);
  padding: 12px 0;
}
.pb4 {
  padding-bottom: 4px;
}

.patient-change {
  padding: 15px;
  > h3 {
    font-weight: normal;
    margin-bottom: 5px;
  }
  > p {
    color: var(--cp-text3);
  }
}
.patient-next {
  padding: 15px;
  background-color: #fff;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 80px;
  box-sizing: border-box;
}

</style>
